package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.cqupt.cxcy.dto.InvestorImpl;
import cn.edu.cqupt.cxcy.model.Project;
import cn.edu.cqupt.cxcy.model.ProjectPage;

/**
 * 管理员后台主页
 * @author yinxu
 * @date 2015年10月17日
 */
public class InveShowPro extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		
		//super.doPost(request, response);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		List<Object> list=(List<Object>)session.getAttribute("user");
		int inve_id=(int)list.get(1);
         InvestorImpl a=new InvestorImpl();
        int sumofproject=a.SumOfProject();
        List<Project> show_2 =new InvestorImpl().returnProjectByid(inve_id,1);//所有非此投资人的项目集合
        List<Project> allprojectshow=a.FirstOpenOfProjectOfSelf();
      
        for(int i=0;i<show_2.size();i++)
        {
        	for(int j=0;j<allprojectshow.size();j++)
        	{
        		if(show_2.get(i).getP_id()==allprojectshow.get(j).getP_id())
        			allprojectshow.remove(j);
        	   
        	}
        }
       
        sumofproject=sumofproject-show_2.size();
         request.setAttribute("sumofall",sumofproject);
         

 		int pt_id_dianzi=new InvestorImpl().returnPt_id("'电子'");
 		System.out.println(pt_id_dianzi);
 		int shengwu=new InvestorImpl().returnPt_id("'生物信息'");
 		int canying=new InvestorImpl().returnPt_id("'餐饮'");
 		int business=new InvestorImpl().returnPt_id("'商务'");	
 		int others=new InvestorImpl().returnPt_id("'其他'");
 		List<Project> allproject=allprojectshow;
 		List<Project> alloffood=new ArrayList<Project>();
 		List<Project> shengwuinfo=new ArrayList<Project>();
 		List<Project> allofbusiness=new ArrayList<Project>();
 		List<Project> allofqita=new ArrayList<Project>();
 		List<Project> allofdianzi=new ArrayList<Project>();
 		List<Map<String,Object>> allshow=new ArrayList<Map<String,Object>>();
 		for(int i=0;i<allprojectshow.size();i++)
 		{   
 			if(allprojectshow.get(i).getPt_id()==pt_id_dianzi)
 				{allofdianzi.add(allprojectshow.get(i));
 				}
 			if(allprojectshow.get(i).getPt_id()==business)
				{allofbusiness.add(allprojectshow.get(i));
				}
 			if(allprojectshow.get(i).getPt_id()==shengwu)
				{shengwuinfo.add(allprojectshow.get(i));
				}
 			if(allprojectshow.get(i).getPt_id()==others)
				{allofqita.add(allprojectshow.get(i));
				}
 			
 			if(allprojectshow.get(i).getPt_id()==canying)
				{alloffood.add(allprojectshow.get(i));
				}
 		}
 		
 		Map<String,String> allinfoofsum=new HashMap<String,String>();
 	
 	    allinfoofsum.put("all",Integer.toString(allprojectshow.size()) );
 		allinfoofsum.put("dianzi", Integer.toString(allofdianzi.size()));
 		allinfoofsum.put("canying", Integer.toString(alloffood.size()));
 		allinfoofsum.put("shangwu", Integer.toString(allofbusiness.size()));
 		allinfoofsum.put("qita", Integer.toString(allofqita.size()));
 		allinfoofsum.put("shengwu", Integer.toString(shengwuinfo.size()));
 		//System.out.println(allproject.size());
 		int acount=0;
 		for(int i=0;i<allproject.size();i++)
 		{
 			Project one=allproject.get(i);
 			
 		
 		Map<String,Object> map=new HashMap<String,Object>();
 		map.put("pro_id", String.valueOf(one.getP_id()));
 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
 		map.put("projectname",one.getP_name() );
 		 acount=a.returnacount(inve_id, one.getP_id(), 2);
 		//System.out.println(acount);
 		
 		map.put("acount",acount);
 		
 			allshow.add(map);
 		}
 		int firstpagesum=1;
 		if(allshow.size()%4==0)
 			firstpagesum=allshow.size()/4;
 		else
 			firstpagesum=(allshow.size()/4)+1;
 		ProjectPage page=new ProjectPage(firstpagesum,1,4);
 		List<Map<String,Object>> alltopage=new ArrayList<Map<String,Object>>();
 		Map<String,Object> middle=new HashMap<String,Object>();
 		for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
 		{
 			if(i<=allshow.size()-1)
 			{middle=allshow.get(i);
 			alltopage.add(middle);}
 			
 			
 		}
 		String projectType=request.getParameter("projecttype");
 		
 		if(projectType!=null)
 		{		 if("pro_all".equals(projectType))
  	         {   
  	    	
  	    	String pagenow=request.getParameter("pagenow");
  	    	
  	    	if(pagenow!=null)
  	    		page.setPagenow(Integer.parseInt(pagenow));
  	    	
  	    	String type=request.getParameter("type");
  	    	if(type!=null)
  	    	{if("up".equals(type))
  	    		{page.setPagenow(page.getPagenow()-1);
  	    		}
  	    	if("down".equals(type))
  	    			page.setPagenow(page.getPagenow()+1);
  	    		
  	    	}
  	    	alltopage.clear();
  	    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
  	 		{
  	 			if(i<=allshow.size()-1)
  	 			{middle=allshow.get(i);
  	 			alltopage.add(middle);}

  	 		}
  	    }
 	    if("pro_dian".equals(projectType))
 	    {    
 	    	
 	    	if(new InvestorImpl().ReturnSumofAlltype(pt_id_dianzi)%4==0)
	 			firstpagesum=new InvestorImpl().ReturnSumofAlltype(pt_id_dianzi)/4;
	 		else
	 			firstpagesum=(new InvestorImpl().ReturnSumofAlltype(pt_id_dianzi)/4)+1;
	    	
	    	page.setPagesum(firstpagesum);
	    	alltopage.clear();
	        allshow.clear();
  	    	String pagenow=request.getParameter("pagenow");
  	    	  allproject=new InvestorImpl().TypeOfAll(pt_id_dianzi);
  	    	if(pagenow!=null)
  	    		page.setPagenow(Integer.parseInt(pagenow));
  	    	
  	    	String type=request.getParameter("type");
  	    	if(type!=null)
  	    	{if("up".equals(type))
  	    		page.setPagenow(page.getPagenow()-1);
  	    		if("down".equals(type))
  	    			page.setPagenow(page.getPagenow()+1);
  	    	}
  	    	for(int i=0;i<allofdianzi.size();i++)
  	 		{
  	 			Project one=allofdianzi.get(i);
  	 			
  	 		
  	 		Map<String,Object> map=new HashMap<String,Object>();
  	 		map.put("pro_id", String.valueOf(one.getP_id()));
  	 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
  	 		map.put("projectname",one.getP_name() );
  	 		 acount=a.returnacount(inve_id, one.getP_id(), 2);
  	 		
  	 		map.put("acount",Integer.toString(acount));
  	 		
  	 		
  	 			allshow.add(map);
  	 		
  	 		}
  	    	
  	    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
  	 		{
  	 			if(i<=allshow.size()-1)
  	 			{middle=allshow.get(i);
  	 			alltopage.add(middle);}
  	 		}
  	    	
  	    	
 	    }
 	   if("pro_qi".equals(projectType))
 	  {   
 		  if(new InvestorImpl().ReturnSumofAlltype(others)%4==0)
	 			firstpagesum=new InvestorImpl().ReturnSumofAlltype(others)/4;
	 		else
	 			firstpagesum=(new InvestorImpl().ReturnSumofAlltype(others)/4)+1;
	    	
 		 page.setPagesum(firstpagesum);
	    	alltopage.clear();
	        allshow.clear();
	    	String pagenow=request.getParameter("pagenow");
	    	  allproject=new InvestorImpl().TypeOfAll(pt_id_dianzi);
	    	if(pagenow!=null)
	    		page.setPagenow(Integer.parseInt(pagenow));
	    	
	    	String type=request.getParameter("type");
	    	if(type!=null)
	    	{if("up".equals(type))
	    		page.setPagenow(page.getPagenow()-1);
	    		if("down".equals(type))
	    			page.setPagenow(page.getPagenow()+1);
	    	}
	    	for(int i=0;i<allofqita.size();i++)
	 		{
	 			Project one=allofqita.get(i);
	 			
	 		
	 		Map<String,Object> map=new HashMap<String,Object>();
	 		map.put("pro_id", String.valueOf(one.getP_id()));
	 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
	 		map.put("projectname",one.getP_name() );
	 		acount=a.returnacount(inve_id, one.getP_id(), 2);
	 		
	 		map.put("acount",Integer.toString(acount));
	 		
	 		
	 			allshow.add(map);
	 		
	 			
	 		}
	    	
	    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
	 		{
	 			if(i<=allshow.size()-1)
	 			{middle=allshow.get(i);
	 			alltopage.add(middle);}
	 		}
	    	
 	    }
 	  if("pro_can".equals(projectType))
 	  {   
 		  if(new InvestorImpl().ReturnSumofAlltype(canying)%4==0)
	 			firstpagesum=new InvestorImpl().ReturnSumofAlltype(canying)/4;
	 		else
	 			firstpagesum=(new InvestorImpl().ReturnSumofAlltype(canying)/4)+1;
	    	
	    	page.setPagesum(firstpagesum);
	    	alltopage.clear();
	        allshow.clear();
	    	String pagenow=request.getParameter("pagenow");
	    	  allproject=new InvestorImpl().TypeOfAll(pt_id_dianzi);
	    	if(pagenow!=null)
	    		page.setPagenow(Integer.parseInt(pagenow));
	    	
	    	String type=request.getParameter("type");
	    	if(type!=null)
	    	{if("up".equals(type))
	    		page.setPagenow(page.getPagenow()-1);
	    		if("down".equals(type))
	    			page.setPagenow(page.getPagenow()+1);
	    	}
	    	for(int i=0;i<alloffood.size();i++)
	 		{
	 			Project one=alloffood.get(i);
	 			
	 		
	 		Map<String,Object> map=new HashMap<String,Object>();
	 		map.put("pro_id", String.valueOf(one.getP_id()));
	 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
	 		map.put("projectname",one.getP_name() );
	 		acount=a.returnacount(inve_id, one.getP_id(), 2);
	 		
	 		map.put("acount",acount);
	 		
	 		
	 			allshow.add(map);
	 		}
	    	
	    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
	 		{
	 			if(i<=allshow.size()-1)
	 			{middle=allshow.get(i);
	 			alltopage.add(middle);}
	 		}
	    }
		if("pro_sheng".equals(projectType))
  {   
		 	
			  if(new InvestorImpl().ReturnSumofAlltype(shengwu)%4==0)
		 			firstpagesum=new InvestorImpl().ReturnSumofAlltype(shengwu)/4;
		 		else
		 			firstpagesum=(new InvestorImpl().ReturnSumofAlltype(shengwu)/4)+1;
		    	
		    	page.setPagesum(firstpagesum);
		    	alltopage.clear();
		        allshow.clear();
		    	String pagenow=request.getParameter("pagenow");
		    	  allproject=new InvestorImpl().TypeOfAll(pt_id_dianzi);
		    	if(pagenow!=null)
		    		page.setPagenow(Integer.parseInt(pagenow));
		    	
		    	String type=request.getParameter("type");
		    	if(type!=null)
		    	{if("up".equals(type))
		    		page.setPagenow(page.getPagenow()-1);
		    		if("down".equals(type))
		    			page.setPagenow(page.getPagenow()+1);
		    	}
		    	for(int i=0;i<shengwuinfo.size();i++)
		 		{
		 			Project one=shengwuinfo.get(i);
		 			
		 		
		 		Map<String,Object> map=new HashMap<String,Object>();
		 		map.put("pro_id", String.valueOf(one.getP_id()));
		 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
		 		map.put("projectname",one.getP_name() );
		 		 acount=a.returnacount(inve_id, one.getP_id(), 2);
		 		
		 		map.put("acount",acount);
		 		
		 		
		 			allshow.add(map);
		 		}
		    	
		    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
		 		{
		 			if(i<=allshow.size()-1)
		 			{middle=allshow.get(i);
		 			alltopage.add(middle);}
		 		}
  	    }
	if("pro_shang".equals(projectType))
	 {   
		  if(new InvestorImpl().ReturnSumofAlltype(business)%4==0)
	 			firstpagesum=new InvestorImpl().ReturnSumofAlltype(business)/4;
	 		else
	 			firstpagesum=(new InvestorImpl().ReturnSumofAlltype(business)/4)+1;
	    	
	    	page.setPagesum(firstpagesum);
	    	alltopage.clear();
	        allshow.clear();
	    	String pagenow=request.getParameter("pagenow");
	    	  allproject=new InvestorImpl().TypeOfAll(pt_id_dianzi);
	    	if(pagenow!=null)
	    		page.setPagenow(Integer.parseInt(pagenow));
	    	
	    	String type=request.getParameter("type");
	    	if(type!=null)
	    	{if("up".equals(type))
	    		page.setPagenow(page.getPagenow()-1);
	    		if("down".equals(type))
	    			page.setPagenow(page.getPagenow()+1);
	    	}
	    	for(int i=0;i<allofbusiness.size();i++)
	 		{
	 			Project one=allofbusiness.get(i);
	 			
	 		
	 		Map<String,Object> map=new HashMap<String,Object>();
	 		map.put("pro_id", String.valueOf(one.getP_id()));
	 		map.put("teamname", new InvestorImpl().returnTeamName(one.getTe_id()));
	 		map.put("projectname",one.getP_name() );
	 		acount=a.returnacount(inve_id, one.getP_id(), 2);
	 		map.put("acount",acount);
	 		
	 		
	 			allshow.add(map);
	 		}
	    	
	    	for(int i=(page.getPagenow()-1)*4;i<=4*page.getPagenow()-1;i++)
	 		{
	 			if(i<=allshow.size()-1)
	 			{middle=allshow.get(i);
	 			alltopage.add(middle);}
	 		}
 		}
 		}
 		/*System.out.println(alltopage.size());
 		for(int i=0;i<alltopage.size();i++)
 			System.out.println(alltopage.get(i).get("acount"));*/
 		request.setAttribute("pro_show",alltopage );
 		
 		request.setAttribute("pageinfoofall", page);
 		request.setAttribute("allinfoofsum", allinfoofsum);
	request.getRequestDispatcher("investor.jsp").forward(request, response);
 		}
 		 
}